package com.javaweb.util.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class PoiHwpfWordUtil {
	
	/* 暂不支持循环表格扩充 */
	/**
	map形如：
	Map<String,String> map = new HashMap<>();
	map.put("bbxrmc","张三");//被保险人名称
	map.put("bbxrdz","国泰路125号");//被保险人地址
	map.put("gzxh","1");//雇主序号
	map.put("mrbf","45632.21");//每人保费
	map.put("swxe","300000");//死亡限额
	map.put("ylxe","20000");//医疗限额
	map.put("wgfxe","1000");//误工费限额
	map.put("yljtxe","200");//医疗津贴
	map.put("mpxx","这是免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息免赔信息");//免赔信息
	map.put("bxf","1.25");//保险费
	map.put("tbyd","这是特别约定\r\r\r我换行了");//特别约定（\n可能需要转换为\r）
	map.put("bjrq","2024-03-28 23:11:43");//报价时间
	inputDotFilePath形如：C:\\Users\\admin\\Desktop\\1\\EL.dot
	outputDocxPath形如：C:\\Users\\admin\\Desktop\\1\\EL.docx
	*/
	public static void dot2Doc(Map<String,String> map,String inputDotFilePath,String outputDocxPath) throws Exception {
        FileInputStream fis = new FileInputStream(inputDotFilePath);
        //XWPFDocument document = new XWPFDocument(fis);//模板文件对应docx
        HWPFDocument document = new HWPFDocument(fis);//模板文件对应doc
        Bookmarks bookmarks = document.getBookmarks();
        List<Range> list = new ArrayList<>();
        List<String> bookmarkNameList =  new ArrayList<>();
        int totalCount = 0,start = 0,end = 0;//累计变量totalCount，起始位置start，结束位置end
        for(int i=0;i<bookmarks.getBookmarksCount();i++) {
        	Bookmark bookmark = bookmarks.getBookmark(i);
        	String bookmarkName = bookmark.getName();//书签名称
        	//System.out.println(bookmarkName+","+bookmark.getStart()+","+bookmark.getEnd());

        	bookmarkNameList.add(bookmarkName);
        	start = bookmark.getStart();
        	end = bookmark.getEnd();
        	
    		Range range = new Range(start+totalCount,end+totalCount,document);
    		totalCount += map.get(bookmarkName).length();
    		list.add(range);
        }
        for(int i=0;i<list.size();i++) {
        	list.get(i).replaceText(map.get(bookmarkNameList.get(i)),false);
        }
		FileOutputStream outputStream = new FileOutputStream(outputDocxPath);
		document.write(outputStream);
		outputStream.close();
        document.close();
        fis.close();
	}
	
	//主要完成向word插入文字和图片
    public static void main(String[] args) throws Exception {
        XWPFDocument document = new XWPFDocument();
        // 创建段落并添加文本
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("Hello, this is a test text.");
        run.setFontFamily("Arial");
        run.setFontSize(12);

        run.addBreak();
        
        InputStream is = new FileInputStream(new File("C:\\Users\\admin\\Desktop\\1.png"));
        run.addPicture(is,Document.PICTURE_TYPE_PNG,"a.png",Units.toEMU(400),Units.toEMU(300));

        // 写入文件
        FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\output.docx");
        document.write(out);
        out.close();
        document.close();
    }

}
